//
//  YCLEmoticonKeyboardPageCell.swift
//  02.表情键盘
//
//  Created by yaochunlin on 16/4/25.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

//定义协议
protocol YCLEmoticonKeyboardPageCellDelegate: NSObjectProtocol {

    //表情按钮点击 代理方法
    func emoticonKeyboardPageCell(cell: YCLEmoticonKeyboardPageCell ,didSelectedEmotion: YCLEmoticonModel)
    
    //删除按钮点击 代理方法
    func didSelectDeleteButton()
}

class YCLEmoticonKeyboardPageCell: UICollectionViewCell {
    
    //设置代理属性
    weak var delegate: YCLEmoticonKeyboardPageCellDelegate?
    
    
    //单页模型数组
    var pageEmoticon: [YCLEmoticonModel]? {
    
        didSet{
        
            //先隐藏所有按钮
            for button in emoticonButtons {
                
                button.hidden = true
            }
            
            //遍历传入的每个模型
            for (index, emoticon) in (pageEmoticon?.enumerate())! {
                
                let emoticonBtn = emoticonButtons[index]
                
                emoticonBtn.emoticon = emoticon
                emoticonBtn.hidden = false
            }
            
        }
    }
    
    //indexpath属性
    var indexPath: NSIndexPath? {
    
        didSet{
        
            debugLabel.text = "当前第 \(indexPath!.section) 组, 第 \(indexPath!.item) 页"
            
            recentInfoLabel.hidden = indexPath?.section != 0
            
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //设置UI
        prepareUI()
    }
    
    private func prepareUI() {
    
        //添加子控件
        addEmoticonButtons()
        contentView.addSubview(deleteButton)
        contentView.addSubview(debugLabel)
        contentView.addSubview(recentInfoLabel)
        
        //设置子控件约束或frame
        debugLabel.frame = bounds
        
        
        
    }
    
    //设置表情按钮的位置
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let margin: CGFloat = 5
        let bottomMargin: CGFloat = 25
        
        let width = (self.frame.size.width - 2 * margin) / CGFloat(YCLEmoticonColumnOfPage)
        
        let height = (self.frame.size.height - bottomMargin) / CGFloat(YCLEmoticonRowOfPage)
        
        
        
        for (index, button) in emoticonButtons.enumerate() {
            
            let x = margin + width * CGFloat(index % YCLEmoticonColumnOfPage)
            let y = height * CGFloat(index / YCLEmoticonColumnOfPage)
            
            button.frame = CGRectMake(x, y, width, height)
            
        }
        
        //添加删除按钮的frame
        let x = CGFloat(YCLEmoticonColumnOfPage - 1) * width + margin
        let y = CGFloat(YCLEmoticonRowOfPage - 1) * height
        
        deleteButton.frame = CGRectMake(x, y, width, height)
        
        //最近使用按钮页label的中心
        recentInfoLabel.center = CGPoint.init(x: 0.5 * bounds.width, y: bounds.height - recentInfoLabel.frame.size.height * 0.5)
    
    }
    
    //创建表情按钮数组
    private func addEmoticonButtons() {
    
        for _ in 0..<YCLEmoticonNumberOfPage {
            
            let btn = emoticonButton()
            
            btn.backgroundColor = UIColor.randomColor()
            
            //添加按钮点击事件
            btn.addTarget(self, action: Selector("emoticonButtonDidClick:"), forControlEvents: UIControlEvents.TouchUpInside)
            
            //按钮添加到contentView
            contentView.addSubview(btn)
            
            //然后添加按钮到表情数组
            emoticonButtons.append(btn)
            
        }
    }
    
    //监听表情按钮点击事件
    @objc private func emoticonButtonDidClick(btn: emoticonButton) {
        
//        print("点击了表情按钮")
        //调用代理方法
       delegate?.emoticonKeyboardPageCell(self, didSelectedEmotion: btn.emoticon!)
    }
    
    @objc private func deleteButtonClick() {
    
        delegate?.didSelectDeleteButton()
    }
    
    //MARK: -- 懒加载
    //测试label
    private lazy var debugLabel: UILabel = {
    
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(20)
        
        label.textColor = UIColor.redColor()
        
        return label
    }()
    
    //最近使用表情页的label
    private lazy var recentInfoLabel:UILabel = {
    
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(12)
        label.textColor = UIColor.grayColor()
        label.text = "最近使用的表情"
        label.sizeToFit()
        
        return label
    }()
    
    //表情按钮数组
    private lazy var emoticonButtons = [emoticonButton]()
    
    //删除按钮
    private lazy var deleteButton: UIButton = {
    
        let btn = UIButton()
        
        btn.setImage(UIImage.init(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        btn.setImage(UIImage.init(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action: Selector("deleteButtonClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    
    
}
